﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.IO;

namespace WiMoGF.Config
{
    class ProjectItem
    {

        private const string NEW_PROJECT_ID_TOKEN = "$newprojectid$";
        private const string PROJECT_NAMESPACE = "$projectnamespace$";


        public const string WIMO_PROJECT_ID = "$wimoprojectid$";
        public const string WEB_PROJECT_ID = "$webhostprojectid$";
        public const string SL_PROJECT_ID = "$slprojectid$";
        public const string ZUNE_PROJECT_ID = "$zunehdprojectid$";

        private const string STR_WiMoCFConfigTemplates = "WiMoGF.Config.Templates.";

        Stream _stream;
        string _name;
        private bool _isBinary;
        private string _fileContents;
        private string _subDirectory;
        private string _projectName;
        public Guid ProjectId { get; set; }


        public ProjectItem(string name, bool binaryFile, string projectName)
            : this(string.Empty, name, binaryFile, projectName)
        { }

        public ProjectItem(string subDirectory, string name, bool binaryFile, string projectName)
        {
            _subDirectory = subDirectory;
            _isBinary = binaryFile;
            _projectName = projectName;
            string resourceName = (subDirectory == string.Empty) ? STR_WiMoCFConfigTemplates + name : String.Format("{0}{1}.{2}", STR_WiMoCFConfigTemplates, _subDirectory.Replace('\\', '.'), name);

            System.Diagnostics.Debug.WriteLine(resourceName);

            _name = name;
            _stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);

            if (!binaryFile)
            {
                using (var rdr = new StreamReader(_stream))
                {
                    _fileContents = rdr.ReadToEnd();
                }

                SetProjectName(projectName);
                ProjectId = SetProjectId();
            }
        }

        public void ReplaceTags(string tag, string value)
        {
            _fileContents = _fileContents.Replace(tag, value);
            SetProjectName(_projectName);
        }

        public void RenameFile(string oldName, string newName)
        {
            _name = _name.Replace(oldName, newName);
        }

        private void SetProjectName(string projectName)
        {
            _fileContents = _fileContents.Replace(PROJECT_NAMESPACE, projectName);
        }

        private Guid SetProjectId()
        {
            var projectId = Guid.NewGuid();
            _fileContents = _fileContents.Replace(NEW_PROJECT_ID_TOKEN, projectId.ToString());

            return projectId;
        }

        public void SetProjectReferenceId(string token, string projectId)
        {
            _fileContents = _fileContents.Replace(token, projectId);
        }

        public void Write(string directory)
        {
            var parentDirectory = _subDirectory == string.Empty ? directory : String.Format(@"{0}\{1}", directory, _subDirectory);

            if (!System.IO.Directory.Exists(parentDirectory))
                System.IO.Directory.CreateDirectory(parentDirectory);

            if (!_isBinary)
                System.IO.File.WriteAllText(parentDirectory + @"\" + _name, _fileContents);
            else
            {
                var bytes = new byte[_stream.Length];
                _stream.Read(bytes, 0, (int)_stream.Length);

                File.WriteAllBytes(parentDirectory + @"\" + _name, bytes);
            }

            _stream.Close();
            
        }
    }
}
